package com.study.langchain4j.api;

import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

/**
 * @author xl-9527
 * @since 2025/10/28
 */
@RestController
@RequestMapping("/ai/chat/vision/low-level")
public class VisionLowLevelChatApi {

    private final ChatModel visionModel;
    private final String resourceBase64;

    public VisionLowLevelChatApi(
            @Qualifier("visionModel") final ChatModel visionModel,
            @Value("image.png") final ClassPathResource resource) {
        this.visionModel = visionModel;
        try {
            this.resourceBase64 = Base64.getEncoder().encodeToString(resource.getContentAsByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/hello")
    public String hello() {
        return visionModel.chat(
                UserMessage.from(
                        TextContent.from("帮我描述一下这个图片是什么"),
                        ImageContent.from(resourceBase64, "png")
                )
        ).aiMessage().text();
    }
}
